package com.eshopkeeper.web.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseSysController;
import com.eshopkeeper.core.util.WebUtil;
import com.eshopkeeper.core.listener.SessionListener;
import com.eshopkeeper.service.sys.SysSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author chenshufeng
 * @Description: 用户会话管理
 * @date 2017年3月7日
 */
@RestController
@Api(value = "会话管理", description = "会话管理")
@RequestMapping(value = "/session", method = RequestMethod.POST)
public class SysSessionController extends BaseSysController {
    @Autowired
    private SysSessionService sysSessionService;

    // 查询会话
    @ApiOperation(value = "查询会话")
    //@RequiresPermissions("sys.session.read")
    @RequestMapping(value = "/read/list")
    public Object get(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameterMap(request);
        Page<?> list = sysSessionService.query(params);
        Long number = SessionListener.getAllUserNumber();
        modelMap.put("userNumber", number); // 用户数大于会话数,有用户没有登录
        return setSuccessModelMap(modelMap, list);
    }

    // 删除会话
    @ApiOperation(value = "删除会话")
    //@RequiresPermissions("sys.session.delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object update(ModelMap modelMap, @RequestParam(value = "account", required = false) String account) {
        sysSessionService.deleteBySessionId(account);
        return setSuccessModelMap(modelMap);
    }
}
